package com.pioneer.swing.ui;

import com.pioneer.beans.Key;
import com.pioneer.beans.Keyed;
import com.pioneer.beans.ManifestKey;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.event.InternalFrameEvent;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JComboBox;
import javax.swing.Box;
import javax.swing.BoxLayout;

/**
 * Title:        Pioneer Metals Application
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Pioneer Metals
 * @author Gary Chen
 * @version 1.0
 */

public class ManifestsFrame extends JInternalFrame {

    public JTable manifestTable;
	public JButton editButton;
	public JButton deleteButton;
	//public JButton newButton;
	public JButton printButton;
	public JComboBox newComboBox;

    public ManifestsFrame() {
    	super(UIConstants.TITLE_MANIFESTS,true,true);
		this.setName(UIConstants.TITLE_MANIFESTS);

    	JPanel mainPanel=new JPanel(new BorderLayout());
		mainPanel.setBorder(BorderFactory.createEmptyBorder(
                                        15, //top
                                        15, //left
                                        5, //bottom
                                        15) //right
										);
		JPanel centerPanel=new JPanel();
		centerPanel.setLayout(new BorderLayout());
		manifestTable=new JTable();
		
		JScrollPane scrollpane = new JScrollPane(manifestTable);
		centerPanel.add(scrollpane,BorderLayout.CENTER);
		JPanel buttonPanel=new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.X_AXIS));
		editButton=new KeyedButton(UIConstants.COMMAND_EDIT_MANIFEST);
		buttonPanel.add(editButton);
		buttonPanel.add(Box.createHorizontalStrut(20));
		deleteButton=new KeyedButton(UIConstants.COMMAND_DELETE_MANIFEST);
		buttonPanel.add(deleteButton);		
		buttonPanel.add(Box.createHorizontalStrut(20));
		printButton=new KeyedButton(UIConstants.COMMAND_PRINT_MANIFEST);
		buttonPanel.add(printButton);
		buttonPanel.add(Box.createHorizontalStrut(20));
		newComboBox=new JComboBox();
		newComboBox.setName(UIConstants.COMMAND_NEW_MANIFEST);
		buttonPanel.add(newComboBox);

		centerPanel.add(buttonPanel,BorderLayout.SOUTH);
		mainPanel.add(centerPanel);		
	   	this.getContentPane().add(mainPanel,BorderLayout.CENTER);
		
    }
	
	public void fireInternalFrameEvent() {
		fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_ACTIVATED);
	}
	
	public class KeyedButton
	extends JButton
	implements Keyed
	{
		private String selectedJobNum;

		KeyedButton(String label)
		{
			super(label);
		}

		protected void fireActionPerformed(ActionEvent event)
		{
			selectedJobNum=getSelectedJobNum();
			super.fireActionPerformed(event);
		}

		public Key getKey()
		{
			return new ManifestKey(selectedJobNum);
		}

	}

	private String getSelectedJobNum()
	{
		int row=manifestTable.getSelectedRow();
		if (row>-1) {
			return manifestTable.getModel().getValueAt(row,0).toString();
		} else {
			// should never happen
			return null;
		}
	}

    public static void main(String[] args) {
        ManifestsFrame mainMenuFrame = new ManifestsFrame();
    }


}